var BSTMG = BSTMG || {};
BSTMG.supplier = {
		init: function(){
			var that = this;
			/*申请列表*/
			that.supplierListReq();
			/*代理商审核*/
			that.check();
		},
		supplierListReq: function(){
			BSTMG.common.hjhPages("/supplier/supplierAudit/search",function(data){
				var userList = data.userList;
				var $tbody = $("#supplierList tbody");
				var $tr = '';
				if(userList.length > 0) {
					for(var i=0;i<userList.length;i++){
						var userInfo = userList[i];
						
						var userEmailOrTell= userInfo.linkEmail;
						if(userEmailOrTell == null || userEmailOrTell == ''){
							userEmailOrTell = userInfo.linkTel;
						}
						$tr += '<tr class="odd">';
						$tr += '<td>'+(i+1)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userEmailOrTell)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.realName)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.belongCompanyName)+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.provinceCodeName) + BSTMG.tools.checkNull(userInfo.cityCodeName) + BSTMG.tools.checkNull(userInfo.areaCodeName) + BSTMG.tools.checkNull(userInfo.address) + '</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(setApplyType(userInfo.applyType))+'</td>';
						$tr += '<td>'+ BSTMG.tools.checkNull(setApplyState(userInfo.applyState))+'</td>';
						if(userInfo.applyState == 0){
							$tr += '<td><a type="button" class="btn btn-primary" href="/supplier/supplierDetail/'+userInfo.supplierId+'">审核</a></td>';
						}
						else if(userInfo.applyState == 1||userInfo.applyState == 2){
							$tr += '<td><a type="button" class="btn btn-primary" href="/supplier/supplierDetail/'+userInfo.supplierId+'">详情</a></td>';
						}
						$tr += '</tr>';
						
					}
					$tbody.html($tr);
				}else {
					var $tr = '<tr class="odd"><td colspan="8">暂无数据！</td></tr>';
					$tbody.html($tr);
				}
			});
		},
		/*代理商审核*/
		check : function(){
			/*审核通过*/
			$('#pass').on('click',function(){
				var supplierId = $('#supplierId').val(),
				pstate = $('#pstate').val();
				toAudit(supplierId,1,pstate);
			})
			/*审核退回*/
			$('#unpass').on('click',function(){
				var supplierId = $('#supplierId').val(),
				pstate = $('#pstate').val();
				toAudit(supplierId,2,pstate);
			})
		}
}

$(function(){
	BSTMG.supplier.init();
});

//设置申请类型
function setApplyType(para){
	if (para == "1") {
		return "代理商";
	} else if (para == "2") {
		return "市总代";
	} else if(para == "3"){
		return "省总代";
	} else if(para == "4"){
		return "软件开发商";
	} else if(para == "5"){
		return "OEM商合作";
	}
}
//设置审核状态
function setApplyState(para){
	if (para == "0") {
		return "待审核";
	} else if (para == "1") {
		return "已通过";
	} else if(para == "2"){
		return "退回";
	} else if(para == "3"){
		return "无效";
	}
}
//审核操作
function toAudit(supplierId,applyState,pstate){
	if(pstate == 1){
		BSTYUN.dialog.warn('已审批！不能重复审批！');
		return;
	}else if(pstate == 2){
		BSTYUN.dialog.warn('已审批！不能重复审批！');
		return;
	}
	$.ajax({
		type : "POST",
		url : "/supplier/updateSupplier",
		async : false,
		data:{
			supplierId:supplierId,
			applyState:applyState,
			applyType : $('#applyType').val()
		},
		success:function(data){
			if(data) {
				BSTYUN.dialog.ok("审核成功！",function(){
					location.href = "/supplier/supplierAudit";
				});
			}else {
				BSTYUN.dialog.warn("审核失败！");
			}
		}
	})
}

////审核结果查询
//function AuditQuery() {
//	// 审核结果查询
//	var applyType = $("#applyType").val();
//	var applyState = $("#applyState").val();
//	$.ajax({
//		type : "POST",
//		url : "/supplier/supplierAudit",
//		data : {
//			applyType : applyType,
//			applyState : applyState
//		},
//		success : function(data, response) {
//			$("#supplierAuditList").empty();
//			var $tr = '';
//			if (data.userList.length > 0) {
//				$.each(data.userList,function(index, deal) {
//					
//					var userEmailOrTell= userInfo.linkEmail;
//					if(userEmailOrTell == null || userEmailOrTell == ''){
//						userEmailOrTell = userInfo.linkTel;
//					}
//					
//					$tr += '<tr class="odd">';
//					$tr += '<td>'+(i+1)+'</td>';
//					$tr += '<td>'+ BSTMG.tools.checkNull(userEmailOrTell)+'</td>';
//					$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.realName)+'</td>';
//					$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.belongCompanyName)+'</td>';
//					$tr += '<td>'+ BSTMG.tools.checkNull(userInfo.provinceCodeName) + BSTMG.tools.checkNull(userInfo.cityCodeName) + BSTMG.tools.checkNull(userInfo.areaCodeName) + BSTMG.tools.checkNull(userInfo.address) + '</td>';
//					$tr += '<td>'+ BSTMG.tools.checkNull(setApplyType(userInfo.applyType))+'</td>';
//					$tr += '<td>'+ BSTMG.tools.checkNull(setApplyState(userInfo.applyState))+'</td>';
//					if(userInfo.applyState == 0){
//						$tr += '<td><a type="button" class="btn btn-primary" href="/supplier/supplierDetail/'+userInfo.supplierId+'">审核</a></td>';
//					}
//					else if(userInfo.applyState == 1||userInfo.applyState == 2){
//						$tr += '<td><a type="button" class="btn btn-primary" href="/supplier/supplierDetail/'+userInfo.supplierId+'">详情</a></td>';
//					}
//					$tr += '</tr>';	
//				});
//				$tbody.html($tr);
//			}else {
//				var $tr = '<tr class="odd"><td colspan="8">暂无数据！</td></tr>';
//				$tbody.html($tr);
//				}
//			}
//	});
//}
//
